package kit4j.view.awt.listener;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import kit4j.common.command.ICommand;
import kit4j.common.command.executor.ICommandExecutor;
import kit4j.common.command.executor.SequentialCommandExecutor;

public class ExecuteCommandListSelectionListener implements
		ListSelectionListener {
	
	private ICommandExecutor executor;
	
	public ExecuteCommandListSelectionListener() {
		//
	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
		try {
			getExecutor().execute();
		} catch (Throwable e1) {
			e1.printStackTrace();
		}
	}

	public ICommandExecutor getExecutor() {
		if(executor == null) {
			executor = new SequentialCommandExecutor();
		}
		return executor;
	}

	public void setExecutor(ICommandExecutor executor) {
		this.executor = executor;
	}
	
	public void addCommand(ICommand cmd) {
		executor.addCommand(cmd);
	}
	
	public void removeCommand(ICommand cmd) {
		executor.removeCommand(cmd);
	}

}
